package com.lemon.core.utils;

import cn.hutool.json.JSONUtil;
import com.lemon.core.web.Result;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import reactor.core.publisher.Mono;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Optional;

/**
 * @author 李猛
 * @datetime 2023/11/6 16:59
 * @description 类对象
 */
public class ResponseUtil {
    /**
     * Http 失败响应方法
     *
     * @param response
     * @param code
     * @param message
     * @return
     */
    public static void fail(HttpServletResponse response, int code, String message) {
        Result<Void> result = Result.fail(code, message);
        String json = JSONUtil.toJsonStr(result);

        PrintWriter writer = null;
        //设置编码个数
        response.setCharacterEncoding("UTF-8");
        //设置响应类型
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.write(json);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    public static void success(HttpServletResponse response, String message) {
        Result<String> result = Result.ok(message);
        String json = JSONUtil.toJsonStr(result);

        PrintWriter writer = null;
        //设置编码个数
        response.setCharacterEncoding("UTF-8");
        //设置响应类型
        response.setContentType("text/html; charset=utf-8");
        try {
            writer = response.getWriter();
            writer.write(json);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    /**
     * Http异步响应失败的方法
     *
     * @param response
     * @param code
     * @param message
     * @return
     */
    public static Mono<Void> fail(ServerHttpResponse response, int code, String message) {
        Result<Void> result = Result.fail(code, message);
        byte[] data = JSONUtil.toJsonStr(result).getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = response.bufferFactory().wrap(data);
        Optional<HttpStatus> first = Arrays.stream(HttpStatus.values()).filter(item -> item.value() == code).findFirst();
        response.setStatusCode(first.get());
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }

    /**
     * Http异步响应成功的方法
     *
     * @param response
     * @param data
     * @return
     */
    public static Mono<Void> success(ServerHttpResponse response, Object data) {
        byte[] bytes = JSONUtil.toJsonStr(Result.ok(data)).getBytes(StandardCharsets.UTF_8);
        DataBuffer buffer = response.bufferFactory().wrap(bytes);
        response.setStatusCode(HttpStatus.OK);
        response.getHeaders().add("Content-Type", "application/json;charset=UTF-8");
        return response.writeWith(Mono.just(buffer));
    }
}
